package com.rbac.controller;

import com.rbac.annotation.RequiresPermission;
import com.rbac.common.Result;
import com.rbac.dto.UserQueryDTO;
import com.rbac.entity.SysUser;
import com.rbac.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@GetMapping("/list")
	@RequiresPermission("system:user:query")
	public Result<PageInfo> getUserList(UserQueryDTO queryDTO) {
		PageInfo userList = userService.getUserList(queryDTO);
		return Result.success(userList);
	}
	
	@GetMapping("/{id}")
	@RequiresPermission("system:user:query")
	public Result<SysUser> getUserById(@PathVariable Long id) {
		SysUser user = userService.getUserById(id);
		if (user != null) {
			return Result.success(user);
		} else {
			return Result.error("用户不存在");
		}
	}
	
	@PostMapping
	@RequiresPermission("system:user:add")
	public Result<String> addUser(@Valid @RequestBody SysUser user) {
		boolean success = userService.addUser(user);
		if (success) {
			return Result.success("新增用户成功");
		} else {
			return Result.error("新增用户失败，用户名可能已存在");
		}
	}
	
	@PutMapping
	@RequiresPermission("system:user:edit")
	public Result<String> updateUser(@Valid @RequestBody SysUser user) {
		boolean success = userService.updateUser(user);
		if (success) {
			return Result.success("更新用户成功");
		} else {
			return Result.error("更新用户失败");
		}
	}
	
	@DeleteMapping("/{id}")
	@RequiresPermission("system:user:delete")
	public Result<String> deleteUser(@PathVariable Long id) {
		boolean success = userService.deleteUser(id);
		if (success) {
			return Result.success("删除用户成功");
		} else {
			return Result.error("删除用户失败");
		}
	}
	
	@PutMapping("/{id}/status")
	@RequiresPermission("system:user:edit")
	public Result<String> updateUserStatus(@PathVariable Long id, @RequestParam Integer status) {
		boolean success = userService.updateUserStatus(id, status);
		if (success) {
			return Result.success("更新用户状态成功");
		} else {
			return Result.error("更新用户状态失败");
		}
	}
	
	// 查询用户已有角色ID
	@GetMapping("/{id}/roles")
	@RequiresPermission("system:user:query")
	public Result<List<Long>> getUserRoleIds(@PathVariable("id") Long userId) {
		List<Long> ids = userService.getRoleIdsByUserId(userId);
		return Result.success(ids);
	}
	
	// 查询用户已有角色名称
	@GetMapping("/{id}/role-names")
	@RequiresPermission("system:user:query")
	public Result<List<String>> getUserRoleNames(@PathVariable("id") Long userId) {
		List<String> roleNames = userService.getRoleNamesByUserId(userId);
		return Result.success(roleNames);
	}
	
	// 分配角色
	@PostMapping("/{id}/roles")
	@RequiresPermission("system:user:edit")
	public Result<String> assignRoles(@PathVariable("id") Long userId, @RequestBody List<Long> roleIds) {
		boolean ok = userService.assignRoles(userId, roleIds);
		return ok ? Result.success("分配角色成功") : Result.error("分配角色失败");
	}
} 